import java.io.IOException;
import java.util.List;

public class mfbt {
    package main.api;

import main.searcher.DocSearcher;
import main.searcher.Result;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
    @WebServlet("/searcher")
    public class DocSearcherServlet extends HttpServlet {
        // 此处的 searcher.DocSearcher 对象也应该是全局唯一的. 因此就给一个 static 修饰
        private static DocSearcher docSearcher = new DocSearcher();
        private ObjectMapper objectMapper = new ObjectMapper();

        @Override
        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
            // 1. 先解析请求, 拿到用户提交的查询词
            String query = request.getParameter("query");
            if(query == null || query.equals("")){
                String msg = "您输入的参数非法 没有获取到query的值";
                System.out.println(msg);
                response.sendError(404,msg);
                return;
            }
            //2.打印一下要搜索的值
            System.out.println("query"+query);
            //3.调用搜索块,来进行搜索
            List<Result> results = docSearcher.search(query);
            //4.把当前的搜索结果进行打包
            response.setContentType("application/json;charset=utf-8");
            objectMapper.writeValue(response.getWriter(),results);
        }

    }

}
